using BuildingBlocks.Abstractions.Permissions;
using Identity.Application.Contracts.Services;
using Identity.Application.PermissionKeyResourceIds.Interfaces;

namespace Identity.Infrastructure.Services
{
    public class PermissionBindingService : IPermissionBindingService
    {
        private readonly IApiPermissionBindingReadService _apiPermissionReadService;
        public PermissionBindingService(IApiPermissionBindingReadService apiPermissionReadService)
        {
            _apiPermissionReadService = apiPermissionReadService;
        }
        public Task<IReadOnlyCollection<PermissionBindingDto>> GetBindingsAsync(string serviceName, CancellationToken ct = default)
        {
            return _apiPermissionReadService.GetBindingsAsync(serviceName, ct);
        }

        public async Task<string> GetMappedPermissionKeyAsync(string resourceId)
        {
            var permissionBinding =await _apiPermissionReadService.GetBindingsByResourceIdAsync(resourceId);
            if (permissionBinding == null) throw new KeyNotFoundException($"无法找到ApiId为:{resourceId}的权限绑定记录");

            return permissionBinding.permissionKey;
        }
    }
}